﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace AppFuscator
{
    internal class UserTariff
    {
        public bool AssemblyMerge { get; set; }
        public bool LibraryMode { get; set; }
        public bool ExternalMethodCallHidding { get; set; }
        public bool EncodeStrings { get; set; }
        public bool Decomposition { get; set; }
        public bool ControlFlow { get; set; }
        public int Remaning { get; set; }
        public string Name { get; set; }

        private static volatile UserTariff instance;
        private static object syncRoot = new object();
        public static UserTariff Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new UserTariff();
                    }
                }

                return instance;
            }
        }

        private UserTariff()
        {
            AssemblyMerge = false;
            LibraryMode = false;
            ExternalMethodCallHidding = false;
            EncodeStrings = false;
            ControlFlow = false;
            Decomposition = false;
        }

        public void LoadFromAnswer(string answer)
        {
            JObject data = Utils.ParseJson(answer);
            if (data == null)
                return;
            if (data.Property("state").Value.ToString() != "ok")
                return;

            JObject rules = data.Property("tariff").Value as JObject;

            AssemblyMerge = rules.Property("AssemblyMerge").Value.ToString().ToLower() == "true";
            LibraryMode = rules.Property("LibraryMode").Value.ToString().ToLower() == "true";
            ExternalMethodCallHidding = rules.Property("ExternalMethodCallHidding").Value.ToString().ToLower() == "true";
            EncodeStrings = rules.Property("EncodeStrings").Value.ToString().ToLower() == "true";
            Decomposition = rules.Property("Decomposition").Value.ToString().ToLower() == "true";
            ControlFlow = rules.Property("ControlFlow").Value.ToString().ToLower() == "true";
            Remaning = int.Parse(rules.Property("Period").Value.ToString().ToLower());
            Name = rules.Property("TariffName").Value.ToString();
        }
    }
}